package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.CardExceptionRecord;
import com.amsp.oa.oademo.service.CardExceptionRecordService;
import com.amsp.oa.oademo.vo.CardExceptionRecordVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "刷卡异常登记表管理")
@RestController
@RequestMapping("/api/card-exception-record")
public class CardExceptionRecordController {

    @Autowired
    private CardExceptionRecordService cardExceptionRecordService;

    @ApiOperation("保存或更新刷卡异常记录")
    @PostMapping("/save")
    public ResponseEntity<Map<String, Object>> saveOrUpdate(@RequestBody CardExceptionRecordVO vo) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = cardExceptionRecordService.saveOrUpdateCardExceptionRecord(vo);
            result.put("code", success ? 200 : 500);
            result.put("message", success ? "保存成功" : "保存失败");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "保存失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("批量保存或更新刷卡异常记录")
    @PostMapping("/batch-save")
    public ResponseEntity<Map<String, Object>> batchSaveOrUpdate(@RequestBody List<CardExceptionRecordVO> voList) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = cardExceptionRecordService.batchSaveOrUpdateCardExceptionRecord(voList);
            result.put("code", success ? 200 : 500);
            result.put("message", success ? "批量保存成功" : "批量保存失败");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "批量保存失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("获取刷卡异常记录详情")
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getDetail(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            CardExceptionRecordVO vo = cardExceptionRecordService.getCardExceptionRecordDetail(id);
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", vo);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("分页查询刷卡异常记录")
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            CardExceptionRecordVO vo) {
        Map<String, Object> result = new HashMap<>();
        try {
            Page<CardExceptionRecord> page = new Page<>(current, size);
            Page<CardExceptionRecordVO> pageResult = cardExceptionRecordService.getCardExceptionRecordPage(page, vo);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", pageResult);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("删除刷卡异常记录")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = cardExceptionRecordService.deleteCardExceptionRecord(id);
            result.put("code", success ? 200 : 500);
            result.put("message", success ? "删除成功" : "删除失败");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "删除失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("批量删除刷卡异常记录")
    @DeleteMapping("/batch")
    public ResponseEntity<Map<String, Object>> batchDelete(@RequestBody List<Long> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = cardExceptionRecordService.batchDeleteCardExceptionRecord(ids);
            result.put("code", success ? 200 : 500);
            result.put("message", success ? "批量删除成功" : "批量删除失败");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "批量删除失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
}